package com.government.company.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.government.area.domain.ParkArea;
import com.government.common.core.domain.BaseTreeVo;
import com.government.common.utils.TreeUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.transaction.annotation.Transactional;
import com.government.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.government.company.mapper.IndustryMapper;
import com.government.company.domain.Industry;
import com.government.company.service.IIndustryService;

/**
 * 行业信息Service业务层处理
 *
 * @author system
 * @date 2023-10-31
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class IndustryServiceImpl implements IIndustryService {
    @Autowired
    private IndustryMapper industryMapper;

    /**
     * 查询行业信息
     *
     * @param id 行业信息主键
     * @return 行业信息
     */
    @Override
    public Industry selectIndustryById(Long id) {
        return industryMapper.selectById(id);
    }

    /**
     * 查询行业信息列表
     *
     * @param industry 行业信息
     * @return 行业信息
     */
    @Override
    public List<Industry> selectIndustryList(Industry industry) {

        QueryWrapper<Industry> wrappers = new QueryWrapper<Industry>();
        wrappers.eq(industry.getIsDeleted(), "0");
        if (StrUtil.isNotBlank(industry.getName())) {
            wrappers.like(industry.getName(), industry.getName());
        }
        return industryMapper.selectList(wrappers);
    }

    /**
     * 新增行业信息
     *
     * @param industry 行业信息
     * @return 结果
     */
    @Override
    public int insertIndustry(Industry industry) {
        if (Objects.nonNull(industry.getParentId()) && industry.getParentId() != 0) {
            Industry industryParent = industryMapper.selectById(industry.getParentId());
            industry.setGrade(industryParent.getGrade() + 1);
        } else {
            industry.setGrade(1L);
        }
        industry.setCreateTime(DateUtils.getNowDate());
        return industryMapper.insert(industry);
    }

    /**
     * 修改行业信息
     *
     * @param industry 行业信息
     * @return 结果
     */
    @Override
    public int updateIndustry(Industry industry) {
        if (Objects.nonNull(industry.getParentId())) {
            Industry industryParent = industryMapper.selectById(industry.getParentId());
            industry.setGrade(industryParent.getGrade() + 1);
        } else {
            industry.setGrade(1L);
        }
        industry.setUpdateTime(DateUtils.getNowDate());
        return industryMapper.updateById(industry);
    }


    /**
     * 删除行业信息信息
     *
     * @param id 行业信息主键
     * @return 结果
     */
    @Override
    public int deleteIndustryById(Long id) {

        Industry industry = industryMapper.selectById(id);
        industry.setIsDeleted("1");
        return industryMapper.updateById(industry);
    }

    /**
     * 查询行业树结构信息
     *
     * @return
     */
    @Override
    public List<BaseTreeVo> industryTree() {

        List<Industry> industryList = industryMapper.selectList(Wrappers.<Industry>lambdaQuery().eq(Industry::getIsDeleted, "0"));
        if (CollectionUtils.isEmpty(industryList)) {

            return null;
        }
        HashMap<Long, List<BaseTreeVo>> allNodeMap = new HashMap<>();

        for (Industry item : industryList) {
            Long parentId = Objects.isNull(item.getParentId()) ? 0 : item.getParentId();
            List<BaseTreeVo> nodeList = allNodeMap.get(parentId);
            if (CollectionUtils.isEmpty(nodeList)) {

                nodeList = Lists.newArrayList();
            }
            BaseTreeVo industryTreeVO = new BaseTreeVo();
            industryTreeVO.setId(String.valueOf(item.getId()));
            industryTreeVO.setValue(item.getId());
            industryTreeVO.setLabel(item.getName());
            nodeList.add(industryTreeVO);
            allNodeMap.put(parentId, nodeList);
        }
        List<BaseTreeVo> rootNode = allNodeMap.get(0L);

        return TreeUtils.buildTree(rootNode, allNodeMap);
    }
}
